package com.xiaochong.tir.manager.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import com.xiaochong.tir.common.data.po.TransactionPair;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.ExchangeTransactionpairMiddleService;
import com.xiaochong.tir.manager.service.TransactionPairService;
import com.xiaochong.tir.manager.util.ListSortUtil;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2018/5/30.
 */
@Controller
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"extrading/")
@Api(value = "交易所-交易对")
public class ExchangeTransactionpairMiddleController {

    @Autowired
    private ExchangeTransactionpairMiddleService exchangeTransactionpairMiddleService;
    @Autowired
    private TransactionPairService transactionPairService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 查询列表
     * @param exchangeTransactionpairMiddle
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @ApiOperation(value = "交易所关联的交易对列表",notes = "交易所关联的交易对列表")
    @PostMapping("traList")
    @ResponseBody
    public Object getTraList(ExchangeTransactionpairMiddle exchangeTransactionpairMiddle,
                             @RequestParam("limit") Integer pageSize,
                             @RequestParam("page")Integer pageIndex){
        PageInfo<ExchangeTransactionpairMiddle> list = exchangeTransactionpairMiddleService.
                selectByEntitys(exchangeTransactionpairMiddle,pageSize,pageIndex);
        ReturnResult<ExchangeTransactionpairMiddle> result = new ReturnResult<ExchangeTransactionpairMiddle>(0,"成功",list.getList(),list.getTotal());
        return result;
    }

    /**
     * 交易所下属货币列表
     * @param id
     * @param exchangeName
     * @param model
     * @return
     */
    @GetMapping("ecList")
    public String exchangeCurrencyList(@RequestParam("id")Integer id, @RequestParam("exchangeName")String exchangeName,Model model){
        model.addAttribute("exchangeName",exchangeName);
        model.addAttribute("id",id);
        return "/transactionpairmiddle/exchangetra";
    }

    /**
     * /新增页面
     * @param model
     * @param id
     * @param exchangeName
     * @return
     */
    @GetMapping("addtrapage")
    public String addTransactionPage(Model model,@RequestParam("id")Integer id, @RequestParam("exchangeName")String exchangeName){
        model.addAttribute("id",id);
        model.addAttribute("exchangeName",exchangeName);
        return "/transactionpairmiddle/cretransaction";
    }

    /**
     * /新增交易对
     * @param checkList
     * @param exchangeId
     * @return
     */
    @ApiOperation(value = "交易所添加交易对关联",notes = "交易所添加交易对关联")
    @PostMapping("addtransaction")
    @ResponseBody
    public Object addTransaction(@RequestParam("checkList")String checkList,
                                 @RequestParam("exchangeId")Integer exchangeId){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增中间表交易对");
        ExchangeTransactionpairMiddle exchangeTransactionpairMiddle = new ExchangeTransactionpairMiddle();
        List<TransactionPair> list = JSON.parseArray(checkList,TransactionPair.class);
        for (int i = 0; i < list.size(); i++) {
            TransactionPair transactionPair = list.get(i);
            exchangeTransactionpairMiddle.setTransactionId(transactionPair.getId());
            exchangeTransactionpairMiddle.setExchangeId(exchangeId);
            exchangeTransactionpairMiddle.setTransactionName(transactionPair.getTransactionName()+"/"+transactionPair.getEnName());
            exchangeTransactionpairMiddle.setTransactionAlias(transactionPair.getTransactionName()+"/"+transactionPair.getEnName());
            //精确查找，判断选中的交易对是否已关联，若已关联就跳过
            List<ExchangeTransactionpairMiddle> middle = exchangeTransactionpairMiddleService
                    .selectByEntityList(exchangeTransactionpairMiddle);
            if(middle.size() > 0 ){//遇到重复的，保留第一个，删除其他
                if(middle.size() > 1){
                    ListSortUtil sortUtil = new ListSortUtil();
                    sortUtil.mySort(middle,"id","asc");
                    for (int j = 1; j < middle.size(); j++) {
                        ExchangeTransactionpairMiddle middle1 = middle.get(i);
                        exchangeTransactionpairMiddleService.deleteByPrimaryKeys(middle1.getId());
                    }
                }
                continue;
            }
            exchangeTransactionpairMiddle.setCreateTime(new Date());
            exchangeTransactionpairMiddle.setEnableFlag(false);
            exchangeTransactionpairMiddle.setSelected(0);
            exchangeTransactionpairMiddle.setQuantifyCurrencySelect(false);
            //新增交易对中间表
            exchangeTransactionpairMiddleService.insertSelective(exchangeTransactionpairMiddle);
        }
        return new ReturnResult<ExchangeTransactionpairMiddle>(0,"成功",1,1);
    }

    /**
     * /查询当前交所中没有的交易对
     * @param transactionPair
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @ApiOperation(value = "查询当前交所中没有的交易对",notes = "根据交易所id查询没有关联的交易对列表")
    @PostMapping("ntralist")
    @ResponseBody
    public ReturnResult<TransactionPair> getTransactionList(TransactionPair transactionPair,
                                                            @RequestParam("limit") Integer pageSize,
                                                            @RequestParam("page")Integer pageIndex){
        PageInfo<TransactionPair> list = transactionPairService.selectByentityLists(transactionPair,pageSize,pageIndex);
        ReturnResult<TransactionPair> result = new ReturnResult<TransactionPair>(0,"成功",list.getList(),list.getTotal());
        return result;
    }

    /**
     * 启用停用
     * @param exchangeTransactionpairMiddle
     * @return
     */
    @ApiOperation(value = "交易所关联的交易对状态修改",notes = "交易所关联的交易对状态修改")
    @PostMapping("enableExtrading")
    @ResponseBody
    public Object enableTransaction(ExchangeTransactionpairMiddle exchangeTransactionpairMiddle){
        redisServer.set(RedisKeysUtils.LOGINNAME,"交易对启用停用");
        exchangeTransactionpairMiddle.setUpdateTime(new Date());
        if(null != exchangeTransactionpairMiddle.getEnableFlag() &&exchangeTransactionpairMiddle.getEnableFlag()){
            exchangeTransactionpairMiddle.setEnableFlag(false);
        }else {
            exchangeTransactionpairMiddle.setEnableFlag(true);
        }
        ExchangeTransactionpairMiddle old =
        exchangeTransactionpairMiddleService.selectByPrimaryKey(exchangeTransactionpairMiddle.getId());
        exchangeTransactionpairMiddleService.updateByPrimaryKey(exchangeTransactionpairMiddle,old);
        return new ReturnResult<ExchangeTransactionpairMiddle>(0,"成功",1,1);
    }

    /**
     * 删除交易对
     * @param id
     * @return
     */
    @ApiOperation(value = "删除交易所、交易对关联",notes = "删除交易所、交易对关联")
    @PostMapping("deleteTra")
    @ResponseBody
    public Object deleteTransactionpairMiddle(@RequestParam("id")Integer id){
        redisServer.set(RedisKeysUtils.LOGINNAME,"删除交易对");
        ExchangeTransactionpairMiddle exchangeTransactionpairMiddle =
                exchangeTransactionpairMiddleService.selectByPrimaryKey(id);
        exchangeTransactionpairMiddleService.deleteByPrimaryKey(id,exchangeTransactionpairMiddle);
        return new ReturnResult<ExchangeTransactionpairMiddle>(0,"成功",1,1);
    }

    /**
     * 交易对精选、取消精选
     * @param exchangeTransactionpairMiddle
     * @return
     */
    @ApiOperation(value = "交易对精选状态修改",notes = "交易对精选状态修改")
    @PostMapping("choicenessTra")
    @ResponseBody
    public Object choicenessTransactionpair(ExchangeTransactionpairMiddle exchangeTransactionpairMiddle){
        redisServer.set(RedisKeysUtils.LOGINNAME,"交易对精选/取消精选");
        exchangeTransactionpairMiddle.setUpdateTime(new Date());
        if(exchangeTransactionpairMiddle.getSelected().equals(1)){
            exchangeTransactionpairMiddle.setSelected(0);
        }else {
            exchangeTransactionpairMiddle.setSelected(1);
        }
        ExchangeTransactionpairMiddle old =
                exchangeTransactionpairMiddleService.selectByPrimaryKey(exchangeTransactionpairMiddle.getId());
        exchangeTransactionpairMiddle.setEnableFlag(old.getEnableFlag());
        exchangeTransactionpairMiddleService.updateByPrimaryKey(exchangeTransactionpairMiddle,old);
        return new ReturnResult<ExchangeTransactionpairMiddle>(0,"成功",1,1);
    }

    /**
     * 设置量化货币
     * @param exchangeTransactionpairMiddle
     * @return
     */
    @ApiOperation(value = "交易对量化货币状态修改",notes = "交易对量化货币状态修改")
    @PostMapping("qcsTra")
    @ResponseBody
    public Object qcs(ExchangeTransactionpairMiddle exchangeTransactionpairMiddle){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改量化货币状态");
        ExchangeTransactionpairMiddle old =
                exchangeTransactionpairMiddleService.selectByPrimaryKey(exchangeTransactionpairMiddle.getId());
        exchangeTransactionpairMiddle.setEnableFlag(old.getEnableFlag());
        exchangeTransactionpairMiddle.setUpdateTime(new Date());
        if(null != exchangeTransactionpairMiddle.getQuantifyCurrencySelect()){
            if(exchangeTransactionpairMiddle.getQuantifyCurrencySelect()){
                exchangeTransactionpairMiddle.setQuantifyCurrencySelect(false);
            }else{
                exchangeTransactionpairMiddle.setQuantifyCurrencySelect(true);
            }

        } else {
            exchangeTransactionpairMiddle.setQuantifyCurrencySelect(true);
        }
        exchangeTransactionpairMiddleService.updateByPrimaryKey(exchangeTransactionpairMiddle,old);
        return new ReturnResult<ExchangeTransactionpairMiddle>(0,"成功",1,1);
    }

    /**
     * 交易对关联货币
     * @param traId
     * @return
     */
    @GetMapping("curTra")
    public Object curTraPage(@RequestParam("traId")Integer traId,Model model){
        model.addAttribute("traId",traId);
        return "/transactionpairmiddle/curTrapage";
    }

    /**
     * 是否需要深度
     * @param id
     * @param isDepth
     * @return
     */
    @ApiOperation(value = "交易对深度状态修改",notes = "交易对深度状态修改")
    @PostMapping("setDepth")
    @ResponseBody
    public Object setDepth(@RequestParam("id")Integer id,
                           @RequestParam(value = "isDepth",required = false) Boolean isDepth){
        redisServer.set(RedisKeysUtils.LOGINNAME,"app设置交易对深度状态");
        ExchangeTransactionpairMiddle middle = new ExchangeTransactionpairMiddle();
        middle.setId(id);
        ExchangeTransactionpairMiddle old = exchangeTransactionpairMiddleService.selectByPrimaryKey(id);
        middle.setUpdateTime(new Date());
        if(null != isDepth){
            if(isDepth){
                middle.setIsDepth(false);
            }else{
                middle.setIsDepth(true);
            }
        }else{
            middle.setIsDepth(true);
        }
        middle.setEnableFlag(old.getEnableFlag());
        exchangeTransactionpairMiddleService.updateByPrimaryKey(middle,old);
        return new ReturnResult<>(0,"成功",1,1);
    }

    /**
     * 批量启用
     * @param checkList
     * @return
     */
    @ApiOperation(value = "交易对批量启用",notes = "交易对批量启用")
    @PostMapping("setBatchStruts")
    @ResponseBody
    public Object setBatchStruts(@RequestParam("checkList")String checkList){
        redisServer.set(RedisKeysUtils.LOGINNAME,"交易对批量启用");
        if(StringUtils.isNotBlank(checkList)){
            List<ExchangeTransactionpairMiddle> list = JSON.parseArray(checkList,ExchangeTransactionpairMiddle.class);
            ExchangeTransactionpairMiddle middle = null;
            ExchangeTransactionpairMiddle old = null;
            if(null != list){
                for (int i = 0; i < list.size(); i++) {
                    middle = new ExchangeTransactionpairMiddle();
                    middle.setId(list.get(i).getId());
                    if(null != list.get(i).getEnableFlag() && !list.get(i).getEnableFlag()){
                        old = exchangeTransactionpairMiddleService.selectByPrimaryKey(middle.getId());
                        middle.setEnableFlag(true);
                        middle.setUpdateTime(new Date());
                        exchangeTransactionpairMiddleService.updateByPrimaryKey(middle,old);
                    }
                }
            }
        }
        return new ReturnResult<>(0,"成功",1,1);
    }
}
